package com.javasee.rain.stock.util;


import com.github.promeg.pinyinhelper.Pinyin;

/**
 * 拼音工具类
 * 基于TinyPinyin库的精确拼音转换
 * 
 * @author : xinyanfei
 * @date : 2025/10/11
 * Copyright (c) 2025, 京东商城 All Rights Reserved.
 */
public class PinyinUtil {

    /**
     * 获取汉字字符串的拼音首字母
     * 
     * @param chinese 中文文本
     * @return 拼音首字母（大写）
     */
    public static String getFirstLetters(String chinese) {
        if (chinese == null || chinese.trim().isEmpty()) {
            return "";
        }
        
        StringBuilder result = new StringBuilder();
        for (char c : chinese.toCharArray()) {
            String pinyin = Pinyin.toPinyin(c);
//            String pinyin = PinyinHelper.getShortPinyin(String.valueOf(c));
            if (pinyin != null && pinyin.length() > 0) {
                result.append(Character.toUpperCase(pinyin.charAt(0)));
            }
        }
        return result.toString();
    }

    /**
     * 获取汉字字符串的完整拼音
     * 
     * @param chinese 中文文本
     * @return 完整拼音（大写，无分隔符）
     */
    public static String getFullPinyin(String chinese) {
        if (chinese == null || chinese.trim().isEmpty()) {
            return "";
        }
        String pinyin = Pinyin.toPinyin(chinese,"");
//        String pinyin = PinyinHelper.getShortPinyin(chinese);
        return pinyin != null ? pinyin.toUpperCase() : "";
    }

    /**
     * 为股票名称生成拼音标识
     * 
     * @param stockName 股票名称
     * @return 拼音首字母（限制长度为6）
     */
    public static String getStockPinyinKey(String stockName) {
        String pinyin = getFirstLetters(stockName);
        return pinyin.length() > 6 ? pinyin.substring(0, 6) : pinyin;
    }

    /**
     * 测试方法 - 用于验证常用股票名称的拼音
     */
    public static void main(String[] args) {
        String[] testNames = {
            "平安银行", "招商银行", "工商银行", "中国银行", "建设银行",
            "比亚迪", "宁德时代", "贵州茅台", "中国平安", "五粮液",
            "美的集团", "格力电器", "万科A", "中国石油", "中国石化",
            "中信证券", "中国建筑", "海螺水泥", "中国中铁", "中国铁建"
        };
        
        System.out.println("股票拼音首字母测试 (基于TinyPinyin):");
        for (String name : testNames) {
            System.out.println(name + " -> " + getStockPinyinKey(name));
        }
    }
}